package com.itheima.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import com.itheima.pojo.UmsResource;
import com.itheima.service.UmsResourceService;
import com.itheima.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/resource")
public class UmsResourceController {
    @Autowired
    private UmsResourceService umsResourceService;

    //添加后台资源
    @PostMapping("/create")
    public Result create(@RequestBody UmsResource umsResource) {
        umsResourceService.create(umsResource);
        return Result.success(null);
    }

    //删除
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        umsResourceService.delete(id);
        return Result.success(null);
    }

    //分页模糊查询
    @GetMapping("list")
    public ResponseEntity list( @RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "5") Integer pageSize,
                        Integer categoryId,
                        String nameKeyword,
                        String urlKeyword) {
       PageResultVo<UmsResource> resultVo = umsResourceService.list(pageNum,pageSize,categoryId,nameKeyword,urlKeyword);
        return ResponseEntity.ok(Result.success(resultVo));
    }

    //查询所有后台资源
    @GetMapping("listAll")
    public Result listAll() {
        List<UmsResource> result = umsResourceService.findAll();
        return Result.success(result);
    }

    //修改后台资源
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Integer id,
                                         @RequestBody UmsResource umsResource) {
        umsResourceService.update(id, umsResource);
        return Result.success(null);
    }

    //根据id获取详情
    @GetMapping("/{id}")
    public Result findid(@PathVariable("id") Integer id) {
        UmsResource umsResource=umsResourceService.findId(id);
        return Result.success(umsResource);
    }
}
